package com.web.service;

import com.google.common.collect.Lists;
import com.web.dao.*;
import com.web.domain.WebResponse;
import com.web.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xukk
 * @date 2017/9/20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    public void getRoleDetail(List<Role> roles) {
        if (roles != null) {
            List<Long> ids = Lists.newArrayList();
            roles.forEach(value ->
                    ids.add(value.getId()));
            List<Role> roles1 = roleMapper.selectMenus(ids);
            List<Role> roles2 = roleMapper.selectPermissions(ids);
            for (Role role : roles) {
                for (Role role1 : roles1) {
                    if (role1.getId().equals(role.getId())) {
                        role.setMenus(role1.getMenus());
                        break;
                    }
                }
                for (Role role2 : roles2) {
                    if (role2.getId().equals(role.getId())) {
                        role.setPermissions(role2.getPermissions());
                        break;
                    }
                }
            }
            //结束for循环
        }

    }

    public WebResponse addRole(Role role, String[] permissionIds) {
        List<Long> menus = Lists.newArrayList();
        List<Long> permissions = Lists.newArrayList();
        if (permissionIds != null) {
            for (String str : permissionIds) {
                if (str.startsWith("m"))
                    permissions.add(Long.valueOf(str.substring(1)));
                else menus.add(Long.valueOf(str));
            }
        }
        if (roleMapper.insertSelective(role) > 0) {
            List<SysMenu> sysMenuList = menuMapper.selectInId(menus);
            List<Permission> permissionList = new ArrayList<>();
            if (permissions.size() > 0) {
                permissionList = permissionMapper.selectInId(permissions);
            }
            List<RoleMenuKey> roleMenuKeyList = Lists.newArrayList();
            List<RolePermissionKey> rolePermissionKeyList = Lists.newArrayList();
            for (SysMenu sysMenu : sysMenuList) {
                RoleMenuKey roleMenuKey = new RoleMenuKey();
                roleMenuKey.setMenuId(sysMenu.getId());
                roleMenuKey.setRoleId(role.getId());
                roleMenuKeyList.add(roleMenuKey);
            }
            for (Permission permission : permissionList) {
                RolePermissionKey rolePermissionKey = new RolePermissionKey();
                rolePermissionKey.setRoleId(role.getId());
                rolePermissionKey.setPermissionId(permission.getId());
                rolePermissionKeyList.add(rolePermissionKey);
            }
            roleMenuMapper.batchInsert(roleMenuKeyList);
            if (rolePermissionKeyList.size() > 0) {
                rolePermissionMapper.batchInsert(rolePermissionKeyList);
            }

            return WebResponse.ok("");
        } else {
            return WebResponse.fail(WebResponse.CF);
        }
        //return WebResponse.ok("");
    }

    public WebResponse updateRole(Role role, String[] permissionIds) {
        if (role.getId() == null || role.getId() == 0) {
            return WebResponse.fail("对象不存在");
        }
        List<Long> menus = Lists.newArrayList();
        List<Long> permissions = Lists.newArrayList();
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        roleMenuExample.createCriteria().andRoleIdEqualTo(role.getId());
        List<RoleMenuKey> roleMenuKeys = roleMenuMapper.selectByExample(roleMenuExample);
        RolePermissionExample rolePermissionExample = new RolePermissionExample();
        rolePermissionExample.createCriteria().andRoleIdEqualTo(role.getId());
        List<RolePermissionKey> rolePermissionKeys = rolePermissionMapper.selectByExample(rolePermissionExample);
        if (permissionIds != null) {
            for (String str : permissionIds) {
                if (str.startsWith("m"))
                    permissions.add(Long.valueOf(str.substring(1)));
                else menus.add(Long.valueOf(str));
            }
        }
        if (roleMapper.updateByPrimaryKeySelective(role) > 0) {
            List<SysMenu> sysMenuList = menus.size() > 0 ? menuMapper.selectInId(menus) : Lists.newArrayList();
            List<Permission> permissionList = permissions.size() > 0 ? permissionMapper.selectInId(permissions) : Lists.newArrayList();
            List<RoleMenuKey> roleMenuKeyList = Lists.newArrayList();
            List<RolePermissionKey> rolePermissionKeyList = Lists.newArrayList();
            if (!compare(roleMenuKeys, sysMenuList)) {
                RoleMenuExample roleMenuExample1 = new RoleMenuExample();
                roleMenuExample1.createCriteria().andRoleIdEqualTo(role.getId());
                roleMenuMapper.deleteByExample(roleMenuExample1);
                for (SysMenu sysMenu : sysMenuList) {
                    RoleMenuKey roleMenuKey = new RoleMenuKey();
                    roleMenuKey.setMenuId(sysMenu.getId());
                    roleMenuKey.setRoleId(role.getId());
                    roleMenuKeyList.add(roleMenuKey);
                }
                roleMenuMapper.batchInsert(roleMenuKeyList);
            }
            if (!compare1(rolePermissionKeys, permissionList)) {
                RolePermissionExample rolePermissionExample1 = new RolePermissionExample();
                rolePermissionExample1.createCriteria().andRoleIdEqualTo(role.getId());
                rolePermissionMapper.deleteByExample(rolePermissionExample1);
                for (Permission permission : permissionList) {
                    RolePermissionKey rolePermissionKey = new RolePermissionKey();
                    rolePermissionKey.setRoleId(role.getId());
                    rolePermissionKey.setPermissionId(permission.getId());
                    rolePermissionKeyList.add(rolePermissionKey);
                }
                if (rolePermissionKeyList.size() != 0) {
                    rolePermissionMapper.batchInsert(rolePermissionKeyList);
                }

            }
            return WebResponse.ok("");
        } else {
            return WebResponse.fail(WebResponse.CF);
        }
    }


    public static boolean compare(List<RoleMenuKey> a, List<SysMenu> b) {
        if (a.size() != b.size()) {
            return false;
        }
        for (int i = 0; i < a.size(); i++) {
            Boolean flag = false;
            for (int j = 0; j < b.size(); j++) {
                if (a.get(i).getMenuId().equals(b.get(i).getId())) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return false;
            }
        }
        return true;
    }

    public static boolean compare1(List<RolePermissionKey> a, List<Permission> b) {
        if (a.size() != b.size()) {
            return false;
        }
        for (int i = 0; i < a.size(); i++) {
            Boolean flag = false;
            for (int j = 0; j < b.size(); j++) {
                if (a.get(i).getPermissionId().equals(b.get(i).getId())) {
                    flag = true;
                    break;
                }
            }
            if (!flag) {
                return false;
            }
        }
        return true;
    }
}
